//
//  CPHomeCollectionController.m
//  News
//
//  Created by chen on 15/3/24.
//  Copyright (c) 2015年 chen. All rights reserved.
//

#import "CPHomeCollectionController.h"
#import "CPHomeCell.h"
#import "CPCellModal.h"
#import "CPNewController.h"
#import "CPTableController.h"
#import "CPCollectionView.h"

@interface CPHomeCollectionController ()
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) NSMutableArray *cellModals;
@end

@implementation CPHomeCollectionController

// 懒加载
- (NSArray *)cellModals{
    if (_cellModals == nil){
        
        _cellModals  = (NSMutableArray *)[CPCellModal CellModals];
    
     }
    return _cellModals;
}


static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self observerNotification];
    
    [self setLayout];
    
    [self addTableVC];
    self.collectionView.backgroundColor = [UIColor colorWithRed:244/256.0 green:244/256.0 blue:244/256.0 alpha:1.0];
    
    
}

// 设置布局
- (void)setLayout {
    CGFloat width = [[UIScreen mainScreen] bounds].size.width / 3 - 2;
    self.flowLayout.itemSize = CGSizeMake(width, width);
    self.flowLayout.minimumInteritemSpacing = 1.0f;
    self.flowLayout.minimumLineSpacing = 1.0f;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}


// 添加tableView的子控制器
- (void)addTableVC {
    
    CPTableController *tableVc = [[CPTableController alloc] init];
    [self addChildViewController:tableVc];
    [self.collectionView addSubview:tableVc.view];
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    [tableVc.tableView setFrame:CGRectMake(-150.f, 0.0f, 150.f, height)];
    
   
    
}

// 监听通知
- (void)observerNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickTableCell:) name:CPModalNotification object:nil];
}

// 通知监听事件
- (void)clickTableCell:(NSNotification *)notification{
    
    NSDictionary *userInfo = notification.userInfo;
    CPCellModal *modal = userInfo[CPModalKey];
    
    for (CPCellModal *temModal in self.cellModals){
        if ( [temModal.title isEqualToString:modal.title]){
            
            return;
        }
        
    }
    
    NSUInteger index = self.cellModals.count - 1;
    [self.cellModals insertObject:modal atIndex:index];
    
    [self.collectionView reloadData];
    
    // 写入 plist 文件
    [modal writeToPlist];
    
}

// 取消监听
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - collection的数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cellModals.count;
 
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CPHomeCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    // 这是从stordBoard 加载出来；
    CPCellModal *modal = self.cellModals[indexPath.row];
    cell.modal = modal;
    return cell ;
    
}


#pragma  mark - collectionView的代理方法
// choice  一行
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // push一个 方法：
    
   
    CPCellModal *modal = self.cellModals[indexPath.row];
    if ([modal.title isEqualToString:@"添加内容"])
    {
        CPCollectionView *view = (CPCollectionView *)self.collectionView;
        
        view.totalWidth = view.leftView.frame.size.width;
        [view touchesEnded:nil withEvent:nil];
        
        
    }else{
        
    CPNewController *newController = [CPNewController newController];
    newController.homeModal = modal;
    [self.navigationController pushViewController:newController animated:YES];
    
    }
}

@end
